package com.green.service.common.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.green.common.page.Pagination;
import com.green.common.util.CreateSqlUtils;
import com.green.dao.common.GetValueDao;
import com.green.service.common.GetValueService;
import com.green.service.util.GetValueUtil;

/**
 * @author hxy
 * @date 2012-2-14
 * 
 */
@Service("getValueService")
public class GetValueServiceImpl extends BaseServiceImpl implements GetValueService {
	@Resource
	private GetValueDao getValueDao;
	private static  Logger logger = Logger.getLogger(GetValueServiceImpl.class);   
	private static String FKCUID="fkcuid";
	@SuppressWarnings("rawtypes")
	@Override
	public Pagination<HashMap> getVlaueList(String sqlId,Pagination<HashMap> pagination, String sqlParams,String orderBy) {
		String sqltemp=GetValueUtil.getSql(sqlId);
		if(StringUtils.isEmpty(sqltemp)){
			logger.error("sqlId值："+sqlId+" 找不到对应的sql");
			 return null;
		}
		
		/**
		 * ******************************如果sql中存在在fkcuid条件则赋值 begin
		 * 如：sql=select * from ....... where ... and  fkcuid and ...
		 * 或者是fkorgid
		 */
		if(sqltemp.indexOf(FKCUID) > 0){
//			Person person=SessionPerson.get();
//			String strSub="";
//			strSub=sqltemp.substring(0,sqltemp.indexOf(FKCUID)+FKCUID.length());
//			strSub+=" ='"+person.getControUnit().getId()+"'";
//			sqltemp=strSub+sqltemp.substring(sqltemp.indexOf(FKCUID)+FKCUID.length());
			sqltemp=sqltemp.substring(0,sqltemp.lastIndexOf("and"));
		}
		
		/**
		 * 拼装条件
		 */
		StringBuffer sql=new StringBuffer(sqltemp);
		CreateSqlUtils.createFilterSql(sqlParams, sql);
		CreateSqlUtils.createSortSql(orderBy, sql, null);
		if(sqlId.equals("getBuildingByGardenIdNew")){
			return getValueDao.getVlaueList(getBuildingByGardenId(sql), pagination);
		}else{
			return getValueDao.getVlaueList(sql.toString(), pagination);
		}
	}
	public Pagination<Object> getVlaueList(String sqlId,Pagination<Object> pagination, String sqlParams) {
		return getValueDao.getVlaueListBySqlId(sqlId, pagination, null);
	}
	
	/**
	 * 特殊处理getBuildingByGardenId 的sql语句 
	 * or building.fregistername like '%%'
	 * @param sql
	 */
	public String getBuildingByGardenId(StringBuffer sql){
		  String sqlStr=sql.toString();
		  if(sqlStr.indexOf("%") > 0){
			  String bulidName=sqlStr.substring(sqlStr.indexOf("%")+1,sqlStr.lastIndexOf("%"));
			  sqlStr=sqlStr.replaceAll("And building.fname", "And (building.fname||building.funitname");
			  sqlStr += " or building.fregistername||building.funitname like '%"+bulidName+"%')";
		  }
		 return sqlStr;
	}
	
	
	@SuppressWarnings("rawtypes")
	@Override
	public List getValueByKey(String sqlid, Map<String, Object> params) {
		List resultList = getValueDao.getListByKey(sqlid,params);
		///System.err.println("come here!!!");
		return resultList;
	}

	
	@Override
	public Pagination<Object> getValueListBySqlId(String sqlid, Pagination<Object> pagination,String sqlParams){
		Map<String, Object> params=CreateSqlUtils.createFilterSqlParams(sqlParams);
		return getValueDao.getVlaueListBySqlId(sqlid,pagination, params);
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public Pagination<Object> getValueListBySqlId(String sqlid, Pagination<Object> pagination,Map sqlParams){
		Map<String, Object> params=CreateSqlUtils.createFilterSqlParams(sqlParams.get("sqlParams").toString());
		params.put("cityname", sqlParams.get("cityname"));
		return getValueDao.getVlaueListBySqlId(sqlid,pagination, params);
	}
	
}
